/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package me.aartikov.alligator.helpers;

import ohos.aafwk.ability.Ability;

import java.util.HashMap;

/**
 * 运行的栈辅助类
 */
public class RunInfoStackHelp {
    /**
     * 记录运行中的所有的ability和fragment
     */
    HashMap<String, Object> runInfoStack = new HashMap<>();

    private static volatile RunInfoStackHelp instance;

    private RunInfoStackHelp() {
        runInfoStack = new HashMap<>();
    }


    /**
     * 获取单例
     * @return RunInfoStackHelp
     */
    public static RunInfoStackHelp getInstance() {
        RunInfoStackHelp lockInstance = instance;
        if (lockInstance == null) {
            synchronized (RunInfoStackHelp.class) {
                lockInstance = instance;
                if (lockInstance == null) {
                    lockInstance = instance = new RunInfoStackHelp();
                }
            }
        }
        return lockInstance;
    }

    /**
     * 添加fraction
     * @param fragment 需要添加的fraction
     */
    public void addFragment(MiFraction fragment) {
        runInfoStack.put(fragment.toString(), fragment);
    }

    /**
     * 添加ability
     * @param ability 需要添加的ability
     */
    public void addAbility(Ability ability) {
        runInfoStack.put(ability.toString(), ability);
    }

    /**
     * 通过标示获取到相应的对象
     * @param tag tag
     * @return Object
     */
    public Object getDate(String tag) {
        return runInfoStack.get(tag);
    }

    /**
     * 清除栈中的ability
     * @param ability ability
     * @return Object
     */
    public Object remove(Ability ability) {
        return runInfoStack.remove(ability.toString());
    }

    /**
     * 清除栈中的fraction
     * @param fragment fraction
     * @return Object
     */
    public Object remove(MiFraction fragment) {
        return runInfoStack.remove(fragment.toString());
    }
}
